function retrieveVideo (data, status) {
	$.ajax( {
		url : 'http://gdata.youtube.com/feeds/api/videos?key=AI39si5mNTMrNzZM997rDSvzZAIzKL9Gqn2VJmlk8Hhm0H3DlKRYvw9UwP3pE5fbev4XEgHusRoYTqFFTcqGkQbfccdEyvAZQQ'
				+ '&category=tricktionary&author=casperushka&v=2&alt=json-in-script',
		success : culc,
		dataType : 'jsonp'
	});
}

var more = false;

function culc (data, status) {
	if(more)
		more = false;
	
	$.each(data.feed.entry, function() {
		var videoId = this.media$group.yt$videoid.$t;
		var tags = this.media$group.media$keywords.$t.toString();
		$("<span>" + videoId + ": " + tags + "</span><br/>").appendTo("#videoList");
		var url = "/video.php/?function=saveVideo&";
		url += "tags=" + tags + "&videoId=" + videoId;
		
		$.ajax({
			  url: url,
			  success: saved()
		});
	});
	
	var link = data.feed.link;
	for(j=0; j<link.length; j++) {
		if(link[j].rel == 'next') {
			more = true;
			$.ajax( {
				url : link[j].href,
				success : culc,
				dataType : 'jsonp'
			});
			return;
		}
	}
}

function saved() {
	// todo
}

function getVideo() {
	var url = "/video.php/?function=getVideo";
	url += "&tags=" + "push%20up";
	url += "&types=" + "Single%20rope";
	url += "&leves=" + "level%201";
	url += "&startIndex=0&limit=10";
	$.ajax({
		  url: url,
		  success: insertVideo
	});

}
function insertVideo(data, status) {
	var videos = data.split("&")[1].split(",");
	
	$.each(videos, function() {
		$.ajax( {
			url : 'http://gdata.youtube.com/feeds/api/videos/' + this 
					+ '?key=key=AI39si5mNTMrNzZM997rDSvzZAIzKL9Gqn2VJmlk8Hhm0H3DlKRYvw9UwP3pE5fbev4XEgHusRoYTqFFTcqGkQbfccdEyvAZQQ' 
					+ 'v=2&alt=json-in-script',
			success : parseVideo,
			dataType : 'jsonp'
		});
	});
}

function parseVideo(data, status) {
	$("<div>" + data.entry.media$group.media$title.$t + "</div>").appendTo("#videoList");
}